home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Technology Demos and Tools.iso / java / demo / ice / Cube.java < prev    next >
Encoding:
Java Source  |  1996-05-17  |  4.6 KB  |  171 lines

  1. // Cube.java
  2. //
  3. // Copyright (C) 1995 Dimension X, Inc.
  4. //   Chris Laurel 10-28-95
  5. //
  6. // This code may be redistributed freely.
  7.  
  8. import java.applet.Applet;
  9. import java.awt.*;
  10. import ice.*;
  11.  
  12.  
  13. public class Cube extends Applet
  14. {
  15.    Graphics3D g3d;
  16.    Model cube;
  17.    float xangle, yangle;
  18.    int lastX, lastY;
  19.    Matrix4 t = new Matrix4();
  20.  
  21.  
  22.    public void init()
  23.    {
  24.       cube = createBox(2, 2, 2);
  25.    }
  26.  
  27.    public void start()
  28.    {
  29.       createGraphicsContext();
  30.       display();
  31.    }
  32.  
  33.    public synchronized void display()
  34.    {
  35.       // Clear the contents of the frame buffer
  36.       g3d.clear(Graphics3D.ColorBuffer);
  37.  
  38.       // Set up the model transformation
  39.       t.identity();
  40.       // rotate it
  41.       t.xrotate(xangle);
  42.       t.yrotate(yangle);
  43.       // translate the cube away from the camera along the z axis
  44.       t.translate(0, 0, -5);
  45.  
  46.       // render
  47.       g3d.concatModelMatrix(t);
  48.       g3d.renderSolid(cube);
  49.       g3d.popModelMatrix();
  50.  
  51.       repaint();
  52.    }
  53.  
  54.    
  55.    // Standard applet methods
  56.    public boolean mouseDown(Event e, int x, int y)
  57.    {
  58.       lastX = x;
  59.       lastY = y;
  60.       return true;
  61.    }
  62.  
  63.    public boolean mouseDrag(Event e, int x, int y)
  64.    {
  65.       yangle += (float) (x - lastX) / (float) size().width * (float) Math.PI;
  66.       xangle += (float) (y - lastY) / (float) size().height * (float) Math.PI;
  67.       lastX = x;
  68.       lastY = y;
  69.       display();
  70.       return true;
  71.    }
  72.  
  73.    public void paint(Graphics g)
  74.    {
  75.       update(g);
  76.    }
  77.  
  78.    public void update(Graphics g)
  79.    {
  80.       if (g3d != null)
  81.      g3d.paint(g, 0, 0);
  82.    }
  83.  
  84.  
  85.    private void createGraphicsContext()
  86.    {
  87.       // Create a new 3D graphics context . . . since we're just
  88.       //   going to be rendering a cube, we don't need a depth
  89.       //   buffer--all necessary hidden surface removal is handled
  90.       //   by backface culling.  Note that we need to have a
  91.       //   regular awt Graphics object in order to create a new
  92.       //   3D graphics context (this will likely change once
  93.       //   Iced Java is ported to the beta JDK.)
  94.       g3d = new Graphics3D(this,
  95.                Graphics3D.ColorBuffer,
  96.                size().width, size().height);
  97.  
  98.       // Set the clear color to be light gray (generally, the same
  99.       //   color as the main browser window)
  100.       g3d.setClearColor(Color.lightGray);
  101.  
  102.       g3d.disableLighting();
  103.  
  104.       // Enable dithering, if necessary.  This will only have an effect
  105.       //   for a non-truecolor display.
  106.       g3d.enableDithering();
  107.    }
  108.  
  109.  
  110.    // Create a box model centered about the origin.
  111.    Model createBox(int width, int height, int depth)
  112.    {
  113.       try {
  114.      // Create a new, empty model with per vertex materials,
  115.      //   no normals (we're just using emissiveColor so we
  116.      //   don't need them for lighting), and no texture
  117.      //   coordinates;
  118.      Model m = new Model(Model.BindingPerVertex,
  119.                  Model.BindingNone,
  120.                  false);
  121.  
  122.      // Add the vertices
  123.      m.addVertex(-width / 2, -height / 2, depth / 2);
  124.      m.addVertex(-width / 2, height / 2, depth / 2);
  125.      m.addVertex(-width / 2, height / 2, -depth / 2);
  126.      m.addVertex(-width / 2, -height / 2, -depth / 2);
  127.      m.addVertex(width / 2, -height / 2, depth / 2);
  128.      m.addVertex(width / 2, height / 2, depth / 2);
  129.      m.addVertex(width / 2, height / 2, -depth / 2);
  130.      m.addVertex(width / 2, -height / 2, -depth / 2);
  131.  
  132.      // Add the materials
  133.      Material mat = new Material(Color.white, Color.black,
  134.                      Color.black, Color.black,
  135.                      1, 1);
  136.      mat.emissive = Color.blue;    m.addMaterial(mat);
  137.      mat.emissive = Color.red;     m.addMaterial(mat);
  138.      mat.emissive = Color.green;   m.addMaterial(mat);
  139.      mat.emissive = Color.white;   m.addMaterial(mat);
  140.      mat.emissive = Color.cyan;    m.addMaterial(mat);
  141.      mat.emissive = Color.yellow;  m.addMaterial(mat);
  142.      mat.emissive = Color.magenta; m.addMaterial(mat);
  143.      mat.emissive = Color.gray;    m.addMaterial(mat);
  144.  
  145.      // Add the faces.  vi is an array which will contain the
  146.      //   vertex indices for each face.  Since each vertex is
  147.      //   mapped to one material, we can use the same array
  148.      //   for the material indices.
  149.      int vi[] = new int[4];
  150.      vi[0] = 0; vi[1] = 1; vi[2] = 2; vi[3] = 3;
  151.      m.polygon(4, vi, vi, null, null); // left (-X)
  152.      vi[0] = 7; vi[1] = 6; vi[2] = 5; vi[3] = 4;
  153.      m.polygon(4, vi, vi, null, null); // right (+X)
  154.      vi[0] = 4; vi[1] = 5; vi[2] = 1; vi[3] = 0;
  155.      m.polygon(4, vi, vi, null, null); // front (+Z)
  156.      vi[0] = 3; vi[1] = 2; vi[2] = 6; vi[3] = 7;
  157.      m.polygon(4, vi, vi, null, null); // back (-Z)
  158.      vi[0] = 1; vi[1] = 5; vi[2] = 6; vi[3] = 2;
  159.      m.polygon(4, vi, vi, null, null); // top (+Y)
  160.      vi[0] = 3; vi[1] = 7; vi[2] = 4; vi[3] = 0;
  161.      m.polygon(4, vi, vi, null, null); // bottom (-Y)
  162.  
  163.      return m;
  164.       } catch (InvalidModelException e) {
  165.      return null;
  166.       }
  167.  
  168.    }
  169.  
  170. }
  171.